$(document).ready(function() {

    $('input[type="text"]').focus(function() {
        this.select();
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');

        }
    });
    var act_search = false;
    $("#btn_search_product").click(function() {
        if (act_search === false) {
            act_search = true;

            $.post("ajax/searchProduct.php", {
                text_search: $('#filter_search_product').val(),
                page: 1
            }, function(res) {
                if (res.type == 0) {
                    $("#status_search").val(1);
                    $('#tbody_product_list').html(res.html);
                    $('#page_product').val(res.page);
                    $('#loadmore_product').val(res.load_more);
                    delete_product();
                    zoom_photo();
                } else {
                    alert(res.msg);
                }
                act_search = false;
            }, "json");

        } else {
            return false;
        }
    });
    $("#reset_search").click(function() {
        $("#status_search").val(0);
        window.location.reload();
    });
    delete_product();
    zoom_photo();
//paging
    var killScroll = false;
    $(window).scroll(function() {
        if ($('#loadmore_product').val() == 1) {
            if ($(window).scrollTop() > ($(document).height() - $(window).height() - 100)) {
                $("#loading").show();
                if (killScroll === false) {
                    killScroll = true;
                    if ($("#status_search").val() == 0) {
                        $.post("ajax/loadMoreProduct.php", {
                            page: $('#page_product').val()

                        }, function(res) {
                            $("#loading").hide();
                            $("#tbody_product_list").append(res.html);
                            $('#page_product').val(res.page);
                            $('#loadmore_product').val(res.load_more);
                            delete_product();
                            zoom_photo();
                            killScroll = false;
                            $("#loading").hide();
                        }, "json");
                    } else {
                        $.post("ajax/searchProduct.php", {
                            text_search: $('#filter_search_product').val(),
                            page: $('#page_product').val()
                        }, function(res) {
                            if (res.type == 0) {
                                $('#tbody_product_list').append(res.html);
                                $('#page_product').val(res.page);
                                $('#loadmore_product').val(res.load_more);
                                delete_product();
                                zoom_photo();

                            } else {
                                alert(res.msg);
                            }
                            $("#loading").hide();
                            killScroll = false;
                        }, "json");
                    }
                } else {
                    return false;
                }
            }
        }
    });
});
function delete_product() {
    var act_del = false;
    $(".btn_delete_product").unbind('click');
    $(".btn_delete_product").click(function() {
        var btn_del = $(this);
        if (confirm("Are you sure you want to delete this product?")) {
            if (act_del === false) {
                act_del = true;
                $.post("ajax/deleteProduct.php", {
                    product_id: $(this).attr('rel')
                }, function(res) {
                    act_del = false;
                    alert(res.msg);
                    if (res.type == 0) {
                        btn_del.parent().parent().remove();
                    }

                }, "json");


            } else {
                return false;
            }
        } else {
            return false;
        }
    });
}
function zoom_photo() {
    $(".photo_product").click(function() {
        $("#photo_zoom").html('<img width="500px" src="' + $(this).attr('rel') + '">');
        $('#modal_photo_zoom').modal('show');
    });
}


